
import java.util.ArrayList;
import java.util.Scanner;

class storeManage {
    public static void main(String[] args) {
        ArrayList<Admin.Store> list;
        list = new ArrayList<>();
        loop:
        while (true) {
            System.out.println("----------------商品管理--------------");
            System.out.println("1：添加商品");
            System.out.println("2：删除商品");
            System.out.println("3：修改商品");
            System.out.println("4：查询商品");
            System.out.println("5:退出系统");
            System.out.println("请输入你的选择");
            Scanner cs = new Scanner(System.in);
            String choose = cs.next();
            switch (choose) {
                case "1":
                    addstore(list);
                    break;
                case "2":
                    deletestore(list);
                    break;
                case "3":
                    updatestore(list);
                    break;
                case "4":
                    querystore(list);
                    break;
                case "5":
                    System.out.println("5:退出系统");
                    break loop;

                default:
                    System.out.println("输入有误");
                    break;
            }


        }


    }

    public static void addstore(ArrayList<Admin.Store> list) {
//利用空参构造创建学生对象
        String id = null;
        Admin.Store s = new Admin.Store();
        Scanner cs = new Scanner(System.in);
        while (true) {
            System.out.println("请输入商品的id");
            String ida = cs.next();
            boolean flag = contions(list, id);
            if (flag) {
                System.out.println("id已经存在，请重新录入");
            } else {
                s.setId(ida);
                break;
            }
        }
        System.out.println("请输入商品名");
        String name = cs.next();
        s.setName(name);
        System.out.println("请输入商品地址");
        String address = cs.next();
        s.setAddress(address);
        System.out.println("请输入商品生产日期");
        String productiondate = cs.next();
        s.setProductiondate(productiondate);
        System.out.println("请输入商品型号");
        String model = cs.next();
        s.setModel(model);
        System.out.println("请输入商品进货价");
        String Incomingprice = cs.next();
        s.setIncomingprice(Incomingprice);
        System.out.println("请输入商品零售价");
        String Retailprice = cs.next();
        s.setRetailprice(Retailprice);
        System.out.println("请输入商品数量");
        String number = cs.next();
        s.setNumber(number);


//添加用户对象

        list.add(s);


        System.out.println("添加商品信息成功");
    }

    public static void deletestore(ArrayList<Admin.Store> list) {

        Scanner cs = new Scanner(System.in);
        System.out.println("删除后无法恢复，请用户确认是否继续删除操作。(输入id)");
        String id = cs.next();
//查询id在集合中的索引
        int index = getIndex(list, id);
        //对index进行判断
        if (index >= 0) {
            list.remove(index);
            System.out.println("id为：" + id + "的商品信息删除成功");
        } else {
            System.out.println("id不存在，删除失败");
        }


    }

    public static void updatestore(ArrayList<Admin.Store> list) {
        Scanner cs = new Scanner(System.in);
        System.out.println("请输入要修改的商品信息的id:");
        String id = cs.next();
        int index = 0;
        getIndex(list, id);
        if (index == -1) {
            System.out.println("要修改的" + id + "不存在，请重新输入");
            return;
        }
        //获取要修改的商品对象
        Admin.Store stu = list.get(index);
        //输入其他信息并修改
        System.out.println("请输入要修改的商品名字");
        String newname = cs.next();
        System.out.println("请输入要修改的商品商品地址");
        String address = cs.next();
        System.out.println("请输入要修改的商品商品生产日期");
        String productiondate = cs.next();
        System.out.println("请输入要修改的商品型号");
        String model = cs.next();
        System.out.println("请输入要修改的商品进货价");
        String Incomingprice = cs.next();
        System.out.println("请输入要修改的商品零售价");
        String Retailprice = cs.next();
        System.out.println("请输入要修改的商品数量");
        String number = cs.next();
        System.out.println("商品信息修改成功");
    }



    public static void  querystore(ArrayList<Admin.Store> list) {
        if (list.size() == 0) {
            System.out.println("当前无用户信息，请添加后查询");
            return;
        }

        //集合中有数据 ，打印表头信息
        System.out.println("id\t\t 商品地址\t商品名\t进货价\t商品型号\t生产日期\t库存 零售价");
        int i;
        for (i = 0; i < list.size(); i++) {
            Admin.Store stu = list.get(i);
            System.out.println(stu.getId() + "\t\t"  + stu.getAddress()+"\t\t"+ stu.getName() + "\t\t" + stu.getIncomingprice() + "\t\t" + stu.getModel() + "\t\t" + stu.getProductiondate() + "\t\t" + stu.getNumber()+ "\t\t" + stu.getRetailprice());
        }


    }
    //判断id是否存在
    public static boolean contions(ArrayList<Admin.Store> list, String id) {
        //循环遍历集合
        //     for (int i = 0; i < list.size(); i++) {
        // Admin.Store stu = list.get(i);
        //  String sid = stu.getId();
        //   if (stu.getId().equals(id)) {
        //     return true;
        //     }
        //   }
        //  return false;
        return getIndex(list,id)>=0;
    }

    //通过id获取索引
    public static int getIndex(ArrayList<Admin.Store> list, String id) {
        for (int i = 0; i < list.size(); i++) {
            Admin.Store stu = list.get(i);
            String sid = stu.getId();
            if (stu.getId().equals(id)) {
                return i;

            }
        }
        return -1;

    }
}
